﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class FriendSpawn : MonoBehaviour
{

    public List<UnitData> m_HireApplyList;

    bool bStartedCoroutine;

    // Use this for initialization
    void Start()
    {
        bStartedCoroutine = false;
    }

    // Update is called once per frame
    void Update()
    {


    }

    public void FriendSpawnExcute()
    {
        if (!bStartedCoroutine)
        {
            StartCoroutine(SpawnTimer());
            bStartedCoroutine = true;
        }
    }

    public void SceneChgedSpawn()
    {
        int iQueue = m_HireApplyList.Count;
        for (int i = 0; i < iQueue; ++i)
        {
            SpawnUnit(GameObject.Find(string.Format("RP{0}",i)).transform.position);
        }
    }

    IEnumerator SpawnTimer()
    {
        SpawnUnit();
        yield return new WaitForSeconds(0.75f);
        if (m_HireApplyList.Count > 0)
            StartCoroutine(SpawnTimer());
        else
            bStartedCoroutine = false;
    }

    void SpawnUnit(Vector3 vPos = default(Vector3))  // SPAWN UNIT  in m_HireApplyList[0] FUNC
    {
        UnitStatus backUnitStatus = new UnitStatus();

        switch (m_HireApplyList[0].ClassStatus.enClass)
        {
            case CLASS_TYPE.TECHNICIAN:
                switch (m_HireApplyList[0].ClassStatus.AssnStatus.enAssnType)
                {
                    case ASSN_TYPE.DEFAULT:
                        backUnitStatus = AssnInit("Engineers", WEAPON_TYPE.PISTOL, vPos);
                        backUnitStatus.m_unitData.arrayTrait[0] = GameObject.Find("DataLogic").GetComponent<DataManager>().GetTraitData("Technician0");
                        break;

                    case ASSN_TYPE.RANGE:
                        backUnitStatus = AssnInit("Medics", WEAPON_TYPE.PISTOL, vPos);
                        backUnitStatus.m_unitData.arrayTrait[0] = GameObject.Find("DataLogic").GetComponent<DataManager>().GetTraitData("Technician1");
                        break;

                    case ASSN_TYPE.CLOSE:
                        backUnitStatus.m_unitData = m_HireApplyList[0];
                        break;
                }
                break;

            case CLASS_TYPE.POLICE:
                switch (m_HireApplyList[0].ClassStatus.AssnStatus.enAssnType)
                {
                    case ASSN_TYPE.DEFAULT:
                        backUnitStatus = AssnInit("Polices", WEAPON_TYPE.PISTOL, vPos, "Recruit");
                        backUnitStatus.m_unitData.arrayTrait[0] = GameObject.Find("DataLogic").GetComponent<DataManager>().GetTraitData("Police0");
                        break;

                    case ASSN_TYPE.RANGE:
                        backUnitStatus = AssnInit("Polices", WEAPON_TYPE.PISTOL, vPos, "Officer");
                        backUnitStatus.m_unitData.arrayTrait[0] = GameObject.Find("DataLogic").GetComponent<DataManager>().GetTraitData("Police1");
                        break;

                    case ASSN_TYPE.CLOSE:
                        backUnitStatus = AssnInit("Polices", WEAPON_TYPE.CLOSE, vPos, "Surpressor");
                        backUnitStatus.m_unitData.arrayTrait[0] = GameObject.Find("DataLogic").GetComponent<DataManager>().GetTraitData("Police2");
                        break;
                }
                break;

            case CLASS_TYPE.SOLDIER:
                switch (m_HireApplyList[0].ClassStatus.AssnStatus.enAssnType)
                {
                    case ASSN_TYPE.DEFAULT:
                        backUnitStatus.m_unitData = m_HireApplyList[0];
                        break;

                    case ASSN_TYPE.RANGE:
                        backUnitStatus.m_unitData = m_HireApplyList[0];
                        break;

                    case ASSN_TYPE.CLOSE:
                        backUnitStatus.m_unitData = m_HireApplyList[0];
                        break;
                }
                break;
        }

        GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().UnitList.Add(backUnitStatus);
        m_HireApplyList.RemoveAt(0);
    }


    UnitStatus AssnInit(string strParent, WEAPON_TYPE weaponType, Vector3 vPos = default(Vector3), string strThis = "")
    {
        GameObject tempObj = new GameObject();

        if (strParent == "Polices")
        {
            switch (strThis)
            {
                case "Recruit":
                    tempObj = Instantiate(Resources.Load("Prefaps/Unit/Friendly/Recruit_0") as GameObject) as GameObject;
                    break;
                case "Officer":
                    tempObj = Instantiate(Resources.Load("Prefaps/Unit/Friendly/Officer_0") as GameObject) as GameObject;
                    break;
                case "Surpressor":
                    break;
            }

            tempObj.transform.parent = GameObject.Find("Polices").transform;
        }
        else if (strParent == "Engineers")
        {
            tempObj = Instantiate(Resources.Load("Prefaps/Unit/Friendly/Engineer_0") as GameObject) as GameObject;
            tempObj.transform.parent = GameObject.Find("Engineers").transform;
        }
        else if (strParent == "Medics")
        {
            tempObj = Instantiate(Resources.Load("Prefaps/Unit/Friendly/Medic_0") as GameObject) as GameObject;
            tempObj.transform.parent = GameObject.Find("Medics").transform;
        }

        switch (weaponType)
        {
            case WEAPON_TYPE.PISTOL:
                tempObj.transform.FindChild("Main").FindChild("Unit").GetComponent<UnitStatus>().m_WeaponData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.PISTOL, "Tokarev");
                break;

            case WEAPON_TYPE.CLOSE:
                tempObj.transform.FindChild("Main").FindChild("Unit").GetComponent<UnitStatus>().m_WeaponData = GameObject.Find("DataLogic").GetComponent<DataManager>().Get_WeaponData_List(WEAPON_TYPE.CLOSE, "Shield");
                break;
        }

        tempObj.transform.FindChild("Main").FindChild("Unit").GetComponent<UnitStatus>().m_unitData = m_HireApplyList[0];

        if (vPos == default(Vector3))
            tempObj.transform.FindChild("Main").FindChild("Unit").position = transform.position;
        else
            tempObj.transform.FindChild("Main").FindChild("Unit").position = vPos;

        //tempObj.FindChild("Main").FindChild("Unit").GetComponent<UnitScript>().m_vecDest = new Vector3(Random.Range(-2f, 2f), -1.5f);
        //tempObj.FindChild("Main").FindChild("Unit").GetComponent<StateMachine>().SetState(new MoveState());

        return tempObj.transform.FindChild("Main").FindChild("Unit").GetComponent<UnitStatus>();
    }
}
